// Copyright 2012 Google Inc. All Rights Reserved.

package com.google.appengine.syncserver.shared;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.annotate.JsonCreator;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.map.JsonMappingException;

import java.io.IOException;
import java.util.logging.Logger;

/**
 * Update request from client.
 */
public class ReadUpdateRequest {

  private static final String START_VERSION = "startVersion";
  private static final String RESPONSE_SIZE = "responseSize";
  private static final String CLIENT_ID = "clientId";
  private static final String PREFERENCES_NAME = "preferencesName";
  private static final String USERNAME = "username";

  private static final Logger log = Logger.getLogger(ReadUpdateResponsePage.class.getName());

  /**
   * Converts serialized json string to ReadUpdateRequest object.
   * 
   * @param str
   * @return ReadUpdateRequest as read from the json string.
   */
  @JsonCreator
  public static ReadUpdateRequest valueOf(String str)
      throws JsonParseException, JsonMappingException, IOException {
    return (ReadUpdateRequest) JsonUtils.stringToObject(str, ReadUpdateRequest.class);
  }

  // The username of the requesting client.
  private final String username;

  // The preferences name as in android.
  private final String preferenceName;

  // Client id of the requester.
  private final long clientId;

  // The size that the client can handle per page.
  private final int responseSize;

  // Version greater than which to get updates from.
  private final long startVersionNumber;

  /**
   * Constructor to be called by the client.
   * 
   * @param username
   * @param preferencesName
   * @param clientId
   * @param responseSize
   * @param versionNumber
   */
  public ReadUpdateRequest(
      @JsonProperty(USERNAME) String username,
      @JsonProperty(PREFERENCES_NAME) String preferencesName,
      @JsonProperty(CLIENT_ID) long clientId, 
      @JsonProperty(RESPONSE_SIZE) int responseSize,
      @JsonProperty(START_VERSION) long versionNumber) {
    this.username = username;
    this.preferenceName = preferencesName;
    this.clientId = clientId;
    this.responseSize = responseSize;
    this.startVersionNumber = versionNumber;
  }

  /**
   * @return The client id of the requester.
   */
  @JsonProperty(CLIENT_ID)
  public long getClientId() {
    return clientId;
  }

  /**
   * @return The preference name as in the android client.
   */
  @JsonProperty(PREFERENCES_NAME)
  public String getPreferenceName() {
    return preferenceName;
  }

  /**
   * @return The response size.
   */
  @JsonProperty(RESPONSE_SIZE)
  public int getResponseSize() {
    return responseSize;
  }

  /**
   * @return The version number from which to send response.
   */
  @JsonProperty(START_VERSION)
  public long getStartVersionNumber() {
    return startVersionNumber;
  }

  /**
   * @return the username.
   */
  @JsonProperty(USERNAME)
  public String getUsername() {
    return username;
  }

  @Override
  public String toString() {
    try {
      return JsonUtils.objectToString(this);
    } catch (JsonGenerationException e) {
      log.severe(e.getMessage());
    } catch (JsonMappingException e) {
      log.severe(e.getMessage());
    } catch (IOException e) {
      log.severe(e.getMessage());
    }
    return null;
  }
}
